﻿Imports System.Data.OleDb
Public Class detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then Return

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MotorcyclesConn").ConnectionString)

        Dim sql = "INSERT INTO PagesViewed (MotorcycleFKID,UserName) VALUES (@f1,@f2)"

        Dim cmd = New OleDbCommand(sql, conn)

        conn.Open()

        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)

        cmd.ExecuteNonQuery()

        conn.Close()
    End Sub

    Protected Sub btn_AddComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddComment.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MotorcyclesConn").ConnectionString)

        Dim sql = "INSERT INTO Comments (Author,Subject,Message,MotorcyclesID) VALUES (@f1,@f2,@f3,@f4)"

        Dim cmd = New OleDbCommand(sql, conn)

        conn.Open()

        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))

        cmd.ExecuteNonQuery()

        conn.Close()

        Response.Redirect("detail.aspx?ID=" & Request.Params("ID"))

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SubmitTag.Click
        Dim MotorcyclesID = Request.Params("ID")

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MotorcyclesConn").ConnectionString)

        conn.Open()

        Dim createTag = tb_CreateTag.Text.Split(",, ,:,;")

        For Each createdTag As String In createTag

            Dim isTagPresentCmd As New OleDbCommand("SELECT * FROM Tags WHERE Tag=@f1", conn)
            isTagPresentCmd.Parameters.AddWithValue("@f1", createdTag)
            Dim dr = isTagPresentCmd.ExecuteReader()
            If Not dr.HasRows Then
                Dim addTagCmd As New OleDbCommand("INSERT INTO Tags (Tag) VALUES (@f1)", conn)
                addTagCmd.Parameters.AddWithValue("@f1", createdTag)
                addTagCmd.ExecuteNonQuery()

            End If

            Dim findTagIDCmd As New OleDbCommand("SELECT ID FROM Tags WHERE Tag=@f1", conn)
            findTagIDCmd.Parameters.AddWithValue("@f1", createdTag)

            Dim newTagDr = findTagIDCmd.ExecuteReader()
            newTagDr.Read()
            Dim newID = newTagDr.Item("ID")

            Dim createAssociationCmd As New OleDbCommand("INSERT INTO TagsJoiner (TagsFKID,MotorcycleFKID) VALUES (@f1,@f2)", conn)
            createAssociationCmd.Parameters.AddWithValue("@f1", newID)
            createAssociationCmd.Parameters.AddWithValue("@f2", MotorcyclesID)
            createAssociationCmd.ExecuteNonQuery()

        Next






        For Each li As ListItem In cbl_Tags.Items
            If (li.Selected = True) Then
                Dim sql = "INSERT INTO TagsJoiner (TagsFKID,MotorcycleFKID) VALUES (@f1,@f2)"
                Dim cmd As New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", li.Value)
                cmd.Parameters.AddWithValue("@f2", MotorcyclesID)
                cmd.ExecuteNonQuery()
            End If
        Next
        conn.Close()
    End Sub
End Class